В Python можно создавать вызываемые объекты не только с помощью функций (def или lambda). Объект также считается вызываемым, если у него реализован магический метод call:
class truncater: def __init__(self, length): self._length = length
def __call__(self, s): return s[0:self._length]
print(truncater(4)('abcdabcd')) # abcd
Поскольку декоратор по сути является функцией высшего порядка, его также можно реализовать в виде вызываемого объекта вместо функции:
В Python можно создавать вызываемые объекты не только с помощью функций (def или lambda). Объект также считается вызываемым, если у него реализован магический метод call:
class truncater: def __init__(self, length): self._length = length
def __call__(self, s): return s[0:self._length]
print(truncater(4)('abcdabcd')) # abcd
Поскольку декоратор по сути является функцией высшего порядка, его также можно реализовать в виде вызываемого объекта вместо функции:
With the help of the Search Filters option, users can now filter search results by type. They can do that by using the new tabs: Media, Links, Files and others. Searches can be done based on the particular time period like by typing in the date or even “Yesterday”. If users type in the name of a person, group, channel or bot, an extra filter will be applied to the searches.
The S&P 500 slumped 1.8% on Monday and Tuesday, thanks to China Evergrande, the Chinese property company that looks like it is ready to default on its more-than $300 billion in debt. Cries of the next Lehman Brothers—or maybe the next Silverado?—echoed through the canyons of Wall Street as investors prepared for the worst.
Библиотека Python разработчика | Книги по питону from id